from jinja2 import Template, FileSystemLoader, Environment
from pywebio.output import put_text, put_error
import os


j2_loader = FileSystemLoader('./templates')
env = Environment(loader=j2_loader)


def check_param(inputs):
    if inputs['extra_tag_flag'] is True and inputs['extra_tag_name'] == '':
        put_error('请输入额外Tag名')
        return False
    if inputs['extra_ingress_flag'] is True and \
            inputs['domain_test'] is None and \
            inputs['prefix_test'] is None and \
            inputs['domain_prod'] is None and \
            inputs['prefix_prod'] is None:
        put_error('请输入入口相关参数')
        return False
    return True


def java_jinja2(inputs):
    if check_param(inputs) is False:
        return
    j2_tmpl = env.get_template('ingress.j2')
    os.mkdir(path='temp/' + inputs['deploy_name'])
    j2_tmpl.stream(
        app='vue',
        ingress_name=inputs['deploy_name'],
        namespace=inputs['namespace'],
        kubesphere_description=inputs['ding_msg'],
        domain=inputs['domain_prod'],
        path=inputs['prefix_prod'],
        svc_name=inputs['deploy_name'],
    ).dump('ingress.yaml')
    result = j2_tmpl.render(
        app='vue',
        ingress_name=inputs['deploy_name'],
        namespace=inputs['namespace'],
        kubesphere_description=inputs['ding_msg'],
        domain=inputs['domain_prod'],
        path=inputs['prefix_prod'],
        svc_name=inputs['deploy_name'],
    )
    print(result)
    put_text(result)


def vue_jinja2(inputs):
    if check_param(inputs) is False:
        return
